/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.data;

import db.genomemap.G_DBDataSource;
import db.genomemap.P_DBDataSource;
import db.genomemap.RDBSettings;

import fs.genomemap.FileSettings;
import fs.genomemap.G_FileDataSource;
import fs.genomemap.P_FileDataSource;

import main.genomemap.Preferences;

/**
 * @author Susanta Tewari
 * @version 1.0
 * @created 17-Nov-2007 12:22:33 AM
 */
public abstract class DataSource {

    /** Field description */
    private int ch_id = -1;

    /**
     * probe/clone order,gene order or sequence order
     */
    private int[] order = null;

    /**
     * Constructs ...
     */
    DataSource() {}

    /**
     * creates an instance of GDataSource based on preferences setting
     * @param ch_id
     * @return
     */
    public static GDataSource getGDataSource(int ch_id) {

        GDataSource gData = null;

        if (Preferences.useFileSettings()) {

            gData = new G_FileDataSource();

            ((G_FileDataSource) gData).setChData(FileSettings.load().getChData(ch_id));

        } else {
            gData = new G_DBDataSource();
        }

        gData.setChId(ch_id);

        return gData;
    }

    public static GDataSource getGDataSource(int ch_id, int[] geneOrder) {

        GDataSource gData = getGDataSource(ch_id);

        gData.setOrder(geneOrder);

        return gData;
    }

    /**
     * creates an instance of PDataSource based on preferences setting
     * @param ch_id
     * @return
     */
    public static PDataSource getPDataSource(int ch_id) {

        PDataSource pData = null;
        ChData chData     = null;

        if (Preferences.useFileSettings()) {

            pData  = new P_FileDataSource();
            chData = FileSettings.load().getChData(ch_id);

        } else {

            pData  = new P_DBDataSource();
            chData = RDBSettings.load().getChData(ch_id);
        }

        pData.setChId(ch_id);
        pData.setChData(chData);
        pData.setChLength(chData.getChLength());
        pData.setCloneLength(chData.getCloneLength());
        pData.setCloneCount(chData.getCloneCount());
        pData.setProbFalsePos(chData.getProbFalsePos());
        pData.setProbFalseNeg(chData.getProbFalseNeg());

        return pData;
    }

    public static PDataSource getPDataSource(int ch_id, int[] probeOrder) {

        PDataSource pData = getPDataSource(ch_id);

        pData.setOrder(probeOrder);
        pData.setProbeCount(pData.getOrder().length);

        return pData;
    }

    public static JDataSource getJDataSource(int ch_id) {

        PDataSource pData = getPDataSource(ch_id);
        GDataSource gData = getGDataSource(ch_id);

        pData.loadData();
        gData.loadData();

        return new JDataSource(gData, pData);
    }

    public static JDataSource getJDataSource(int ch_id, int[] probeOrder) {

        JDataSource jData = getJDataSource(ch_id);

        jData.setOrder(probeOrder);

        return jData;
    }

    /**
     * for Genetic Map:RFLP data and Physical Map HYBRIDIZATION data
     */
    public abstract int[][] getdata();

    /**
     * probe/clone order,gene order or sequence order
     */
    public int[] getOrder() {
        return order;
    }

    /**
     * probe/clone order,gene order or sequence order
     *
     * @param newVal
     */
    public void setOrder(int[] newVal) {
        order = newVal;
    }

    public int getChId() {
        return ch_id;
    }

    /**
     *
     * @param newVal
     */
    public void setChId(int newVal) {
        ch_id = newVal;
    }

    /**
     * loads data
     */
    public abstract void loadData();
}
